<?php
namespace WifiSwitch\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        R('User/loginStatusCheck',array(3));

        $userGrade = $_SESSION['WifiPagesUserGrade'];
        $manageRegin = $_SESSION['manageRegin'];

        $userName = $_SESSION['WifiPagesLoginStatus'];


        //1分钟内是否在线
        $d=time()-60;
        $map['isOnLine'] = array('gt',$d);
        $map['deviceBelong'] = array('eq',$userName);

        $sendInfo=D('send_info');
        if($userGrade=='1'){
//            echo $userName;
            $deviseInfo=$sendInfo->where($map)->where(array('deviceType'=>6))->select();
        }else if($userGrade=='2'){
            $deviseInfo=$sendInfo->where($map)->where(array('regionNum'=>$manageRegin,'deviceType'=>6))->select();
        }else if($userGrade=='3'){
            $deviseInfo=$sendInfo->where($map)->where(array('buildingNum'=>$manageRegin,'deviceType'=>6))->select();
        }
//        echo $sendInfo->getLastSql();die;
//        $sendInfo=D('send_info');
//        if($userGrade=='1'){
//            $deviseInfo=$sendInfo->where(array('deviceType'=>6,'deviceBelong'=>$userName))->select();
//        }else if($userGrade=='2'){
//            $deviseInfo=$sendInfo->where(array('regionNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
//        }else if($userGrade=='3'){
//            $deviseInfo=$sendInfo->where(array('buildingNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
//        }
//        dump($deviseInfo);die;
        $this->assign('name',$userName);
        $this->assign('info',$deviseInfo);
        // $this->assign('userName',$_SESSION['loginStatus']);
        $this->display();
    }
    public function leave(){
        R('User/loginStatusCheck',array(3));

        $userGrade = $_SESSION['WifiPagesUserGrade'];
        $manageRegin = $_SESSION['manageRegin'];

        $userName = $_SESSION['WifiPagesLoginStatus'];


        //1分钟内是否在线
        $d=time()-60;
        $map['isOnLine'] = array('lt',$d);

        $sendInfo=D('send_info');
        if($userGrade=='1'){
            $deviseInfo=$sendInfo->where($map)->where(array('deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='2'){
            $deviseInfo=$sendInfo->where($map)->where(array('regionNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='3'){
            $deviseInfo=$sendInfo->where($map)->where(array('buildingNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }

//        dump($deviseInfo);die;
        $this->assign('info',$deviseInfo);
        // $this->assign('userName',$_SESSION['loginStatus']);
        $this->display();
    }
    public function all(){
        R('User/loginStatusCheck',array(3));

        $userGrade = $_SESSION['WifiPagesUserGrade'];
        $manageRegin = $_SESSION['manageRegin'];

        $userName = $_SESSION['WifiPagesLoginStatus'];

        $sendInfo=D('send_info');
        if($userGrade=='1'){
            $deviseInfo=$sendInfo->where(array('deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='2'){
            $deviseInfo=$sendInfo->where(array('regionNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='3'){
            $deviseInfo=$sendInfo->where(array('buildingNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }
//        dump($deviseInfo);die;
        $this->assign('info',$deviseInfo);
        // $this->assign('userName',$_SESSION['loginStatus']);
        $this->display();
    }

    public function indexRefresh(){
        R('User/loginStatusCheck',array(3));

        $userGrade = $_SESSION['WifiPagesUserGrade'];
        $manageRegin = $_SESSION['manageRegin'];


        $userName = $_SESSION['WifiPagesLoginStatus'];
        //1分钟内是否在线
        $d=time()-60;
        $map['isOnLine'] = array('gt',$d);

        $sendInfo=D('send_info');
        if($userGrade=='1'){
            $deviseInfo=$sendInfo->where($map)->where(array('deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='2'){
            $deviseInfo=$sendInfo->where($map)->where(array('regionNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='3'){
            $deviseInfo=$sendInfo->where($map)->where(array('buildingNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }

//        $sendInfo=D('send_info');
//        if($userGrade=='1'){
//            $deviseInfo=$sendInfo->where(array('deviceType'=>6,'deviceBelong'=>$userName))->select();
//        }else if($userGrade=='2'){
//            $deviseInfo=$sendInfo->where(array('regionNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
//        }else if($userGrade=='3'){
//            $deviseInfo=$sendInfo->where(array('buildingNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
//        }
//        dump($deviseInfo);die;
        $this->ajaxReturn(json_encode($deviseInfo),"json");
    }
    public function leaveRefresh(){
        R('User/loginStatusCheck',array(3));

        $userGrade = $_SESSION['WifiPagesUserGrade'];
        $manageRegin = $_SESSION['manageRegin'];

        $userName = $_SESSION['WifiPagesLoginStatus'];
        //1分钟内是否在线
        $d=time()-60;
        $map['isOnLine'] = array('lt',$d);

        $sendInfo=D('send_info');
        if($userGrade=='1'){
            $deviseInfo=$sendInfo->where($map)->where(array('deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='2'){
            $deviseInfo=$sendInfo->where($map)->where(array('regionNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='3'){
            $deviseInfo=$sendInfo->where($map)->where(array('buildingNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }

//        dump($deviseInfo);die;
        $this->ajaxReturn(json_encode($deviseInfo),"json");
    }
    public function allRefresh(){
        R('User/loginStatusCheck',array(3));

        $userGrade = $_SESSION['WifiPagesUserGrade'];
        $manageRegin = $_SESSION['manageRegin'];


        $userName = $_SESSION['WifiPagesLoginStatus'];

        $sendInfo=D('send_info');
        if($userGrade=='1'){
            $deviseInfo=$sendInfo->where(array('deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='2'){
            $deviseInfo=$sendInfo->where(array('regionNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }else if($userGrade=='3'){
            $deviseInfo=$sendInfo->where(array('buildingNum'=>$manageRegin,'deviceType'=>6,'deviceBelong'=>$userName))->select();
        }
//        dump($deviseInfo);die;
        $this->ajaxReturn(json_encode($deviseInfo),"json");
    }

    public function editDevice(){
        R('User/loginStatusCheck',array(3));
        $this->display();
    }
    public function editInfo(){
        R('User/loginStatusCheck',array(2));
        $deviceId=I('post.deviceId');
        $send_info=M('send_info');
        $deviseInfo=$send_info->where(array('id'=>$deviceId))->find();
        $this->ajaxReturn(json_encode($deviseInfo),"json");
    }

    public function removeDevice(){
        R('User/loginStatusCheck',array(2));
        $deviceId=I('post.deviceId');
        $send_info=M('send_info');
        $deviseInfo=$send_info->where(array('id'=>$deviceId))->delete();
        $this->ajaxReturn(json_encode($deviseInfo),"json");
    }
    public function addDevice(){
        R('User/loginStatusCheck',array(3));
        $chipID=I('post.chipID');
        $send_info=M('send_info');
        $data["deviceBelong"] = $_SESSION['WifiPagesLoginStatus'];
        $deviceBelong = $send_info->where(array('chipId'=>$chipID))->field("deviceBelong")->find();

        if($deviceBelong['devicebelong'] == "0"){
            $deviseInfo=$send_info->where(array('chipId'=>$chipID))->save($data);
        }else{
            $deviseInfo = false;
        }
        $this->ajaxReturn(json_encode($deviseInfo),"json");
    }
    public function saveInfo(){
        R('User/loginStatusCheck',array(2));
        $info=$_POST;
        $id = $_POST['deviceId'];
        $send_info = M("send_info");
        $send_info -> where (array('id' =>$id)) ->save($info);
        $status = array ('status'=>1,);
        $this->ajaxReturn(json_encode($status));
    }

    public function getDetail(){
        $id =$_POST['deviceId'];
        $return_info=D('return_info');
        $forthwith_info=$return_info->where(array('deviceId'=>$id))->order('time desc')->limit(15)->select();
//        $forthwith_info['time'] = date("Y-m-d h:i:s" ,$forthwith_info['time']);

        $this->ajaxReturn(json_encode($forthwith_info));

    }



}